package com.offcn.webui.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.webui.service.OrderServiceFeign;
import com.offcn.webui.vo.resp.OrderFormInfoSubmitVo;
import com.offcn.webui.vo.resp.ReturnPayConfirmVo;
import com.offcn.webui.vo.resp.TOrder;
import com.offcn.webui.vo.resp.UserRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @Auther: yao
 * @Date: 2020/10/28 14:31
 * @Description:
 */
@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderServiceFeign orderServiceFeign;

    @RequestMapping("/save")
    public String saveOrder(OrderFormInfoSubmitVo orderFormInfoSubmitVo, HttpSession session) {
        //1.判断用户是否登录
        UserRespVo userRespVo = (UserRespVo) session.getAttribute("sessionMember");
        if (userRespVo == null) {
            return "redirect:/login.html";
        }
        //2.从session获得确认订单对象
        ReturnPayConfirmVo returnPayConfirmVo = (ReturnPayConfirmVo) session.getAttribute("returnConfirmSession");
        if(returnPayConfirmVo==null){
            return "redirect:/login.html";
        }
        //3.设置项目ID，当前回报对象ID，购买数量
        orderFormInfoSubmitVo.setProjectid(returnPayConfirmVo.getProjectId());
        orderFormInfoSubmitVo.setReturnid(returnPayConfirmVo.getId());
        orderFormInfoSubmitVo.setRtncount(returnPayConfirmVo.getNum());

        orderFormInfoSubmitVo.setAccessToken(userRespVo.getAccessToken());    //注意：一定不要缺失
        //4.执行保存订单
        AppResponse<TOrder> response = orderServiceFeign.createOrder(orderFormInfoSubmitVo);
        TOrder order = response.getData();
        log.debug("订单ID:{},支付金额:{}", order.getOrdernum(), order.getMoney());

        return "/member/minecrowdfunding";
    }
}
